package com.ht.temp.controller;


import com.ht.temp.entity.AlertReport;
import com.ht.temp.service.IAlertReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mayke_zhou
 * @since 2018-12-26
 */
@RestController
@RequestMapping("/alertReportController")
public class AlertReportController {

    @Autowired
    private IAlertReportService alertReportService;


    @RequestMapping(value = "/selectByAlertReport", method = RequestMethod.GET)
    public Map<String,Object> selectByAlertReport(AlertReport report, Integer pageNow) {
        int alertReportsCount = alertReportService.selectCountByPage(report);
        int pageCount = 0;
        //limit从哪一页开始查
        int pageBegin = 0;
//        多少页
        int pageNum = 0;
        //每页显示多少条
        int pageshow = 10;


        //如果没有传入当前页数,默认为0
        if (pageNow==null||pageNow==0){
            pageNow=1;
        }

        if (alertReportsCount > 0) {
            if (alertReportsCount % pageshow != 0) {
                pageNum = alertReportsCount / pageshow + 1;
                pageCount = alertReportsCount;
                pageBegin = (pageNow-1) * pageshow;
            } else {
                pageNum = alertReportsCount / pageshow ;
                pageCount = alertReportsCount;
                pageBegin = (pageNow-1) * pageshow;
            }

        }


        List<AlertReport> alertReports = alertReportService.selectByAlertReport(report, pageBegin);

        //分页后的数据放入map中
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("data", alertReports);
        map.put("pageNum", pageNum);
        map.put("pageNow", pageNow);
        map.put("pageCount", pageCount);
        return map;
    }


    @RequestMapping(value = "/selectByCount", method = RequestMethod.GET)
    public Integer selectByCount(AlertReport report, Integer pageNow) {
        int alertReportsCount = alertReportService.selectCountByPage(report);
        return alertReportsCount;
    }

    @RequestMapping(value = "/getAlertReportByDay", method = RequestMethod.POST)
    public List<AlertReport> getAlertReportByDay(String  time, Integer deviceId) {
        return alertReportService.getAlertReportByDay(time,deviceId);
    }
}
